package com.atguigu.blog.web;

import com.atguigu.blog.pojo.Article;
import com.atguigu.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 文章修改页
     * @param id 文章id
     * @return
     */
    @GetMapping("/articleUpdate/{id}")
    public String articleUpdate(@PathVariable("id") String id, Map<String, Object> map) {
        Article byId = articleService.getById(id);
        map.put("articleById", byId);
        return "articleUpate";
    }

    /**
     * 修改文章
     * @param id
     * @param article
     * @return
     */
    @PostMapping("/aritcleUpdateById/{id}")
    public String aritcleUpdateById(@PathVariable("id") String id, Article article) {
        article.setId(id);
        article.setUpdateDate(new Date());
        articleService.updateById(article);
        return "redirect:/ucenter";
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") String id) {
        articleService.removeById(id);
        return "redirect:/ucenter";
    }

}
